/**
 * @brief
 *
 * @version  0.1
 * @author   wuxingchuang
 * @date     2025.06.04
 */
#include <gtest/gtest.h>
#include <highfive/H5File.hpp>
#include <nlohmann/json.hpp>

#include "DBManager/DBAPI.h"
#include "DBManager/IService/Input/Node/INodeService.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"

using namespace SG::DBManager;

TEST (HighFiveTest, WriteJson)
{
    DB::ServiceFactory->ClearDatabase ();

    using json = nlohmann::json;

    // 构造一个 JSON 对象
    json j;
    j["name"]   = "Alice";
    j["age"]    = 30;
    j["skills"] = { "C++", "Python", "HDF5" };

    // 转为字符串
    std::string json_str = j.dump ();

    // 写入到 HDF5 文件
    HighFive::File file ("data.h5", HighFive::File::Overwrite);
    file.createDataSet<std::string> ("aaa/my_json", HighFive::DataSpace::From (json_str)).write (json_str);

    // 读取 JSON 字符串
    std::string json_from_hdf5;
    file.getDataSet ("aaa/my_json").read (json_from_hdf5);

    // 解析为 JSON 对象
    json j2 = json::parse (json_from_hdf5);

    // std::cout << "读取到的 JSON:" << j2.dump (4) << std::endl;
}

TEST (HighFiveTest, WriteBinary)
{
    // 构造一些二进制数据
    std::vector<uint8_t> binary_data = { 0xDE, 0xAD, 0xBE, 0xEF, 0x12, 0x34 };

    // 写入到 HDF5 文件
    HighFive::File file ("binary_example.h5", HighFive::File::Overwrite);
    file.createDataSet<uint8_t> ("bin", HighFive::DataSpace::From (binary_data)).write (binary_data);

    // 读取回二进制数据
    std::vector<uint8_t> read_data;
    file.getDataSet ("bin").read (read_data);

    // 打印以验证
    // std::cout << "读取到的二进制数据: ";
    for (auto v : read_data)
    {
        printf ("%02X ", v);
    }
    std::cout << std::endl;
}